// pages/account/index.js
const app = getApp();
Page({

     /**
      * 页面的初始数据
      */
	data: {
		userInfo: {},
		my_reward_person: 0,
		my_reward_money: { mag: 0, rmb: 0 },
		data: {},
		page: 1, //当前页码
		is_wxpay:0,
		setting:{}
	},

     /**
      * 生命周期函数--监听页面加载
      */
	onLoad: function (options) {
		let that = this;
		//是否开启微信支付
		app.connect('index/setting', {}, function (res) {
			that.setData({
				is_wxpay: res.is_wxpay,
				setting:res
			})
		});
		app.login(function (userInfo) {
			that.setData({
				userInfo: userInfo.data
			});
			//收到的赞赏人数
			app.connect('my/my_reward_person', {
				uid: that.data.userInfo.uid
			}, function (e) {
				that.setData({
					my_reward_person: e
				})
			});
			//收入
			app.connect('my/my_reward_money', {
				uid: that.data.userInfo.uid
			}, function (e) {
				that.setData({
					my_reward_money: e
				})
			}, true);
			//数据列表
			app.connect('content/index', {
				my_uid: that.data.userInfo.uid
			}, function (e) {
				that.setData({
					data: e.data
				})
				console.log(e);
			});
		});

	},



     /**
      * 页面上拉触底事件的处理函数
      */
	onReachBottom: function (e) {
		let that = this;
		app.connect('content/index', {
			my_uid: that.data.userInfo.uid,
			page: that.data.page + 1
		}, function (e) {

			if (e.data.length == 0) {
				wx.showToast({
					title: '到底啦',
					icon: 'none'
				})
				return false;
			}
			var data = that.data.data;
			that.setData({
				data: data.concat(e.data),
				page: that.data.page + 1
			})

		}, true);
	},

     /**
      * 用户点击右上角分享
      */
	onShareAppMessage: function () {

	},
	/**
	 * 点击申请
	 */
	shenqing: function (e) {
		let that = this;
		var content_id = e.currentTarget.dataset.content_id;
		var index = e.currentTarget.dataset.index;
		app.connect('account/shenqing', {
			uid: that.data.userInfo.uid,
			openid: that.data.userInfo.openId,
			content_id: content_id
		}, function (options) {
			var data = that.data.data;
			data[index].is_apply = 1;
			//加载数据
			that.setData({
				data: data
			})
		}, true);
	},
	/**
	 * 删除
	 */
	del: function (e) {
		var content_id = e.currentTarget.dataset.id;
		var index = e.currentTarget.dataset.index;
		let that = this;
		wx.showModal({
			title: '确认要删除吗',
			content: '',
			success: function (res) {
				if (res.confirm) {
					app.connect("account/delete_content", {
						uid: that.data.userInfo.uid,
						openid: that.data.userInfo.openId,
						content_id: content_id
					}, function (res) {
						if (res.code == 1) {
							var data = that.data.data;
							data.splice(index, 1);
							that.setData({
								data: data
							})
							wx.showToast({
								title: '删除成功',
								icon: 'none'
							})
						}
					});
				} else if (res.cancel) {

				}
			}
		})
	},
	share: function (e) {
		var content_id = e.currentTarget.dataset.content_id;
		wx.showActionSheet({
			itemList: ['发送微信好友', '生成分享图', '取消'],
			success: function (res) {
				//分享给微信好友
				if (res.tapIndex == 0) {
					wx.showShareMenu({
						withShareTicket: true,
						success:function(e){
							console.log(e);
						}
					})
					//生成分享图
				} else if (res.tapIndex == 1) {
					wx.navigateTo({
						url: '../add/success?content_id=' + content_id,
					})
				}
			},
			fail: function (res) {
				console.log(res.errMsg)
			}
		})
	},
	onShareAppMessage: function (res) {
		let that = this;
		if (res.from == 'menu') {
			return {
                    title: '帅哥美女潮人红人都在玩',
				path: 'pages/index/index',
			}
		}
		var content_id = res.target.dataset.content_id;
		var imgUrl = res.target.dataset.imgurl;
		return {
			title: '@' + that.data.userInfo.nickName + ' 在麦颜值发表的作品',
			path: 'pages/look/index?content_id=' + content_id + '&type=1',
			imageUrl:imgUrl
		}
	},
	/**
	 * 更换主页背景图
	 */
	changeImg:function(){
		let that = this;
		wx.showActionSheet({
			itemList: ['更换主页背景'],
			success: function (res) {
				if(res.tapIndex == 0) {
					wx.chooseImage({
						count: 1, // 默认9
						sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
						sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
						success: function (res) {
							// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
							var tempFilePaths = res.tempFilePaths[0]

							wx.uploadFile({
								url: app.globalData.url + '/account/changeHomePageImg', //仅为示例，非真实的接口地址
								filePath: tempFilePaths,
								name: 'sendImg',
								formData: {
									'uid': that.data.userInfo.uid,
									'openid': that.data.userInfo.openId
								},
								success: function (res) {
									//var data = res.data

									console.log(res);
									var userInfo = that.data.userInfo;
									userInfo['home_page_img'] = res.data;
									that.setData({
										userInfo: userInfo
									});
									app.login();
								},
								complete: function (e) {
									console.log(e);
								}
							})
						}
					})
				}
			},
			fail: function (res) {
				console.log(res.errMsg)
			}
		})
	
	},
	lookImg: function (e) {

		var content_id = e.currentTarget.dataset.content_id;
		var img_id = e.currentTarget.dataset.img_id;
		wx.navigateTo({
			url: '../look/index?content_id=' + content_id + "&img_id=" + img_id,
		})
	}
})